<?php
/**
 * Class that operate on table 'things_to_do'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class ThingsToDoMySqlDAO implements ThingsToDoDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return ThingsToDoMySql 
	 */
	public function load($thingsToDo){
		$sql = 'SELECT things_to_do.* ,tc.article_name FROM things_to_do  left join things_article tc on things_to_do.article_id=tc.article_id  WHERE things_id = "'.$thingsToDo->things_id.'"';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}
	
	public function thingstodo_images($thingsToDo){
		$sql = 'SELECT * FROM things_images WHERE things_id = "'.$thingsToDo->things_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM things_to_do';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function searchby_thingstodo($thingsToDo){
		$sql = 'SELECT * FROM things_to_do WHERE '.$thingsToDo->search_by.' like"%'.$thingsToDo->search_by_value.'%" order by '.$thingsToDo->order_by.' '.$thingsToDo->sort_order.' limit '.$thingsToDo->results_per_page.' offset '.$thingsToDo->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function searchbycount($thingsToDo){
		$sql = 'SELECT count(*) FROM things_to_do WHERE '.$thingsToDo->search_by.' like"%'.$thingsToDo->search_by_value.'%" order by '.$thingsToDo->order_by.' '.$thingsToDo->sort_order.' limit '.$thingsToDo->results_per_page.' offset '.$thingsToDo->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	public function queryAll_thingstodo($thingsToDo){
		$sql = 'SELECT * FROM things_to_do order by '.$thingsToDo->order_by.' '.$thingsToDo->sort_order.' limit '.$thingsToDo->results_per_page.' offset '.$thingsToDo->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function thingstodo_count($thingsToDo){
		$sql = 'SELECT count(*) FROM things_to_do order by '.$thingsToDo->order_by.' '.$thingsToDo->sort_order.' limit '.$thingsToDo->results_per_page.' offset '.$thingsToDo->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param thingsToDo primary key
 	 */
	public function delete($thingsToDo){
		$sql = 'UPDATE things_to_do SET status = "'.$thingsToDo->status.'"WHERE things_id = "'.$thingsToDo->things_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	public function remove($thingsToDo){
		$sql = 'DELETE FROM things_images where image_id = "'.$thingsToDo->image_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param ThingsToDoMySql thingsToDo
 	 */
	public function insert($thingsToDo){
		$sql = 'INSERT INTO things_to_do (article_id, things_place, things_location, description, created_date, updated_date) VALUES ("'.$thingsToDo->article_id.'", "'.$thingsToDo->things_place.'", "'.$thingsToDo->things_location.'", "'.$thingsToDo->description.'", "'.$thingsToDo->created_date.'", "'.$thingsToDo->updated_date.'")';
		$sqlQuery = new SqlQuery($sql);
		$id = $this->executeInsert($sqlQuery);	
		$thingsToDo->things_id = $id;
		return $id;
	}
	
	public function insert_image($thingsToDo){
		$sql = 'INSERT INTO things_images (things_id, things_image_title, image_name, things_image, created_date, updated_date) VALUES ("'.$thingsToDo->things_id.'", "'.$thingsToDo->things_image_title.'", "'.$thingsToDo->image_name.'", "'.$thingsToDo->things_image.'", "'.$thingsToDo->created_date.'", "'.$thingsToDo->updated_date.'")';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		$id = $this->executeInsert($sqlQuery);	
		$thingsToDo->things_id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param ThingsToDoMySql thingsToDo
 	 */
	public function update($thingsToDo){
		$sql = 'UPDATE things_to_do SET article_id = ?, things_place = ?, things_location = ?, description = ?, updated_date = ?, status = ? WHERE things_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($thingsToDo->article_id);
		$sqlQuery->set($thingsToDo->things_place);
		$sqlQuery->set($thingsToDo->things_location);
		$sqlQuery->set($thingsToDo->description);
		$sqlQuery->set($thingsToDo->updated_date);
		$sqlQuery->setNumber($thingsToDo->status);
		$sqlQuery->setNumber($thingsToDo->things_id);
		return $this->executeUpdate($sqlQuery);
	}
	
	public function update_image($thingsToDo){
		$sql = 'UPDATE things_images SET things_image_title = ?, image_name = ?, things_image = ?, updated_date = ?, status = ? WHERE things_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($thingsToDo->things_image_title);
		$sqlQuery->set($thingsToDo->image_name);
		$sqlQuery->set($thingsToDo->things_image);
		$sqlQuery->set($thingsToDo->updated_date);
		$sqlQuery->setNumber($thingsToDo->status);
		$sqlQuery->setNumber($thingsToDo->things_id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM things_to_do';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByArticleId($value){
		$sql = 'SELECT * FROM things_to_do WHERE article_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByThingsPlace($value){
		$sql = 'SELECT * FROM things_to_do WHERE things_place = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByThingsLocation($value){
		$sql = 'SELECT * FROM things_to_do WHERE things_location = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBythingsImageTitle($value){
		$sql = 'SELECT * FROM things_to_do WHERE things_imagename = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByImageName($value){
		$sql = 'SELECT * FROM things_to_do WHERE image_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByThingsImage($value){
		$sql = 'SELECT * FROM things_to_do WHERE things_image = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM things_to_do WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM things_to_do WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM things_to_do WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByArticleId($value){
		$sql = 'DELETE FROM things_to_do WHERE article_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByThingsPlace($value){
		$sql = 'DELETE FROM things_to_do WHERE things_place = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByThingsLocation($value){
		$sql = 'DELETE FROM things_to_do WHERE things_location = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBythingsImageTitle($value){
		$sql = 'DELETE FROM things_to_do WHERE things_imagename = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByImageName($value){
		$sql = 'DELETE FROM things_to_do WHERE image_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByThingsImage($value){
		$sql = 'DELETE FROM things_to_do WHERE things_image = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM things_to_do WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM things_to_do WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM things_to_do WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return ThingsToDoMySql 
	 */
	protected function readRow($row){
		$thingsToDo = new ThingsToDo();
		
		$thingsToDo->things_id = $row['things_id'];
		$thingsToDo->article_id = $row['article_id'];
		$thingsToDo->things_place = $row['things_place'];
		$thingsToDo->things_location = $row['things_location'];
		$thingsToDo->things_image_title = $row['things_image_title'];
		$thingsToDo->image_name = $row['image_name'];
		$thingsToDo->things_image = $row['things_image'];
		$thingsToDo->article_name = $row['article_name'];
		$thingsToDo->description = $row['description'];
		$thingsToDo->created_date = $row['created_date'];
		$thingsToDo->updated_date = $row['updated_date'];
		$thingsToDo->status = $row['status'];

		return $thingsToDo;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return ThingsToDoMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>